package com.semidata.trp.decision.config;

import java.io.File;

import com.semidata.rtd.core.config.DefaultValue;
import com.semidata.rtd.core.config.DefaultValueClass;
import com.semidata.rtd.core.config.DefaultValueHandler;
import com.semidata.rtd.core.config.Handler;
import com.semidata.rtd.core.config.Property;
import com.semidata.rtd.core.config.UrlHandler;

public class Config extends com.semidata.rtd.core.config.Config {

	private static String DEFAULT_CONFIG_FILE = "conf/decision.conf";
	private static Config config = null;

	public static void init(String configFile) {
		if (configFile == null)
			configFile = Thread.currentThread().getContextClassLoader()
			        .getResource(DEFAULT_CONFIG_FILE).getPath();

		config = new Config(configFile);
	}

	public static class DefaultLogDir implements DefaultValueHandler {

		@Override
		public String value() {
			File file = new File(Thread.currentThread().getContextClassLoader()
			        .getResource(DEFAULT_CONFIG_FILE).getPath());
			file = new File(file.getParentFile().getParentFile(),
			        "logs/decision");
			return file.getAbsolutePath();
		}

	}

	public static Config getConfig() {
		return config;
	}

	private Config(String configFile) {
		initConfig(configFile);
	}

	@Property("http.port")
	@DefaultValue("6000")
	private int httpPort;

	@Property("http.threads")
	@DefaultValue("16")
	private int httpThreads;

	@Property(value = "log.root")
	@DefaultValueClass(DefaultLogDir.class)
	private String logRoot;

	@Property("log.debug.enabled")
	@DefaultValue("false")
	private boolean logDebugEnabled;

	@Property("url.business")
	@DefaultValue("/business/")
	@Handler(UrlHandler.class)
	private String urlBusiness;

	@Property("url.budget")
	@DefaultValue("/budget/")
	@Handler(UrlHandler.class)
	private String urlBudget;
	
	@Property("url.report")
	@DefaultValue("/report/")
	private String urlReport;

	@Property("url.business.json")
	@DefaultValue("/business_json/")
	@Handler(UrlHandler.class)
	private String urlBusinessJson;

	@Property("url.budget.json")
	@DefaultValue("/budget_json/")
	@Handler(UrlHandler.class)
	private String urlBudgetJson;

	@Property("url.decision")
	@DefaultValue("/decision/")
	@Handler(UrlHandler.class)
	private String urlDecision;

	@Property("url.monitor.impression")
	@Handler(UrlHandler.class)
	private String urlMonitorImpression;

	@Property("url.monitor.click")
	@Handler(UrlHandler.class)
	private String urlMonitorClick;

	@Property("iplib.file")
	@DefaultValue("data/iplib.txt")
	private String ipLibFile;

	@Property("redis.hosts")
	@DefaultValue("127.0.0.1:3306")
	private String redisHosts;

	@Property("task.timeout")
	@DefaultValue("10")
	private int taskTimeout;

	@Property("task.threads")
	@DefaultValue("16")
	private int taskThreads;

	@Property("job.max")
	@DefaultValue("0")
	private long jobMaxCount;

	@Property("redis.connect.timeout")
	@DefaultValue("2000")
	private int redisConnectTimeout;
	
	@Property("redis.task.timeout")
	@DefaultValue("50")
	private int redisTaskTimeout;
	
	@Property("redis.threads")
	@DefaultValue("16")
	private int redisThreads;
	
	@Property("redis.connect.num")
	@DefaultValue("3")
	private int redisConnectNum;
	
	@Property("bid.algorithm.timeout")
	@DefaultValue("10")
	private int bidAlgorithmTimeout;
	
	@Property("timeout.request")
	@DefaultValue("10")
	private int timeoutRequest;
	
	@Property("timeout.frequency")
	@DefaultValue("10")
	private int timeoutFrequency;
	
	@Property("timeout.filter")
	@DefaultValue("50")
	private int timeoutFilter;
	
	@Property("timeout.select")
	@DefaultValue("10")
	private int timeoutSelect;
	
	@Property("timeout.response")
	@DefaultValue("10")
	private int timeoutResponse;
	
	
	public int getTimeoutRequest() {
		return timeoutRequest;
	}

	public int getTimeoutFrequency() {
		return timeoutFrequency;
	}

	public int getTimeoutFilter() {
		return timeoutFilter;
	}

	public int getTimeoutSelect() {
		return timeoutSelect;
	}

	public int getTimeoutResponse() {
		return timeoutResponse;
	}

	public int getBidAlgorithmTimeout () {
		return bidAlgorithmTimeout;
	}
	public int getRedisConnectNum () {
		return redisConnectNum;
	}
	public int getRedisThreads() {
		return redisThreads;
	}
	public int getRedisTaskTimeout () {
		return redisTaskTimeout;
	}
	
	public int getRedisConnectTimeout() {
		return redisConnectTimeout;
	}
	public String[] getRedisHosts() {
		return redisHosts.split(";");
	}

	public int getHttpPort() {
		return httpPort;
	}

	public int getHttpThreads() {
		return httpThreads;
	}

	public String getLogRoot() {
		return logRoot;
	}

	public boolean isLogDebugEnabled() {
		return logDebugEnabled;
	}

	public String getUrlBusiness() {
		return urlBusiness;
	}

	public String getUrlBudget() {
		return urlBudget;
	}
	
	public String getUrlReport() {
		return urlReport;
	}

	public String getUrlBusinessJson() {
		return urlBusinessJson;
	}

	public String getUrlBudgetJson() {
		return urlBudgetJson;
	}

	public String getUrlDecision() {
		return urlDecision;
	}

	public String getUrlMonitorImpression() {
		return urlMonitorImpression;
	}

	public String getUrlMonitorClick() {
		return urlMonitorClick;
	}

	public String getIpLibFile() {
		return ipLibFile;
	}

	public int getTaskThreads() {
		return taskThreads;
	}

	public int getTaskTimeout() {
		return taskTimeout;
	}

	public long getJobMaxCount() {
		return jobMaxCount;
	}

	@Override
	public String toString() {
		return "Config [httpPort=" + httpPort + ", httpThreads=" + httpThreads
		        + ", logRoot=" + logRoot + ", logDebugEnabled="
		        + logDebugEnabled + ", urlBusiness=" + urlBusiness
		        + ", urlBudget=" + urlBudget + ", urlBusinessJson="
		        + urlBusinessJson + ", urlBudgetJson=" + urlBudgetJson
		        + ", urlDecision=" + urlDecision + ", urlMonitorImpression="
		        + urlMonitorImpression + ", urlMonitorClick=" + urlMonitorClick
		        + ", ipLibFile=" + ipLibFile + ", redisHosts=" + redisHosts
		        + ", taskTimeout=" + taskTimeout + ", taskThreads="
		        + taskThreads + "]";
	}

	public static void main(String[] args) {
		init(null);
		System.out.println(getConfig().toString());
	}

}